package cn.js.sangu.unitStore.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 说明：接口返回统一格式包装类
 * 作者：linshuo
 * 日期：2024/8/7 8:43
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder //构建者模式创建对象
public class R {

    public static final String CODE_SUCCESS = "200";
    public static final String CODE_AUTH_ERROR = "401"; //无权限
    public static final String CODE_NOTFOUND = "404"; //无资源
    public static final String CODE_SYS_ERROR = "500";//系统错误

    public static final String REQUEST_SUCCESS = "请求成功";
    public static final String REQUEST_FAIL = "请求失败";

    private String code;
    private String msg;
    private Object data;

    public static R ok(){
        return new R(CODE_SUCCESS,REQUEST_SUCCESS,null);
    }

    public static R ok(Object data){
        return new R(CODE_SUCCESS,REQUEST_SUCCESS,data);
    }


    public static R fail(){
        return new R(CODE_SYS_ERROR,REQUEST_FAIL,null);
    }

    public static R fail(String msg){
        return new R(CODE_SYS_ERROR,msg,null);
    }

    public static R fail(String code,String msg){
        return new R(code,msg,null);
    }

    public static R fail(String code,String msg,Object data){
        return new R(code,msg,data);
    }


    public static R success(){
        return R.builder().code(CODE_SUCCESS).msg(REQUEST_SUCCESS).build(); //链式创建对象
    }

}
